/*
2021-8-11
https://www.acwing.com/problem/content/240/
*/ 
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;
const int N=30005;
int p[N],sz[N],d[N];

void init()
{
    for(int i=1;i<N;i++)
    {
        p[i]=i;
        sz[i]=1;
    }
}

int find(int x)
{
    if(x!=p[x])
    {
        int root=find(p[x]);
        d[x]+=d[p[x]];
        p[x]=root;
    }
    return p[x];
}

int main()
{
    init();
    int T;
    cin>>T;
    while(T--)
    {
        char op[2];
        int a,b;
        cin>>op>>a>>b;
        
        if(op[0]=='M')
        {
            int pa=find(a),pb=find(b);
            d[pa]=sz[pb];
            sz[pb]+=sz[pa];
            p[pa]=pb;
        }
        else
        {
            int pa=find(a),pb=find(b);
            if(pa!=pb) puts("-1");
            else cout<<max(0,abs(d[a]-d[b])-1)<<endl;
        }
        
    }
    return 0;
}
